package com.cold.demo.rabbitmq.demo.work;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WorkSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(Object message){
        String routingKey = WorkConfig.QUEUE_NAME;
        if (message instanceof Message) {
            rabbitTemplate.send(routingKey, (Message) message);
        } else {
            MessageConverter converter = rabbitTemplate.getMessageConverter();
            Message msg = converter.toMessage(message, new MessageProperties());
            rabbitTemplate.send(routingKey, msg);
        }
    }

}
